package com.lkwlw.tplms.action;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.lkwlw.tplms.system.dao.mapper.AdmininfoMapper;
import com.lkwlw.tplms.system.dao.model.Admininfo;
import com.lkwlw.tplms.system.dao.model.AdmininfoExample;
import com.lkwlw.tplms.system.dao.model.AdmininfoExample.Criteria;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport implements SessionAware{
	
	private static final long serialVersionUID = 8114647043341536009L;

	private Admininfo adminBean;
	
	private AdmininfoMapper loginMapper;
	
	private String loginMessage;
	
	private Map session;
	
	public String getLoginMessage() {
		return loginMessage;
	}

	public void setLoginMessage(String loginMessage) {
		this.loginMessage = loginMessage;
	}

	public String execute()throws Exception {
		AdmininfoExample example = new AdmininfoExample();
		
		Criteria criteria = example.createCriteria();
		criteria.andUsernameEqualTo(adminBean.getUsername()).andPasswdEqualTo(adminBean.getPasswd());
		List<Admininfo> adminList = loginMapper.selectByExample(null,example);
		if (adminList != null && adminList.size() == 1) {
			loginMessage = "login success";//;
			session.put("admin", adminList.get(0));
			loginMessage = "";
			return SUCCESS;
		}
		loginMessage = "用户名或密码错误！";
		return INPUT;
	}
	/**
	public void validate(){
		if (adminBean.getUsername().isEmpty()) {
			addFieldError("adminBean.userName", "用户名不能为空！");
		}
		if (adminBean.getPasswd().isEmpty()) {
			addFieldError("adminBean.passWd", "密码不能为空！");
		}
	}
	**/
	public Admininfo getAdminBean() {
		return adminBean;
	}
	
	public void setAdminBean(Admininfo adminBean) {
		this.adminBean = adminBean;
	}
	
	public AdmininfoMapper getLoginMapper() {
		return loginMapper;
	}
	
	public void setLoginMapper(AdmininfoMapper loginMapper) {
		this.loginMapper = loginMapper;
	}

	@Override
	public void setSession(Map session) {
		this.session = session;
	}
	
	
}
